#include "SMyTreeView.h"

void SMyTreeView::Construct(const FArguments& InArgs){

	FArguments Arguments = InArgs;

	// 初始化Arguments
	TSharedPtr<UTreeItemData> ZhangSan = MakeShareable(new UTreeItemData());
	ZhangSan->MyName = FString(TEXT("张三"));
	TSharedPtr<UTreeItemData> ZhangSan1 = MakeShareable(new UTreeItemData());
	ZhangSan1->MyName = FString(TEXT("张三1"));
	
	ZhangSan->Children.Add(ZhangSan1);
	
	TSharedPtr<UTreeItemData> LiSi = MakeShareable(new UTreeItemData());
	LiSi->MyName = FString(TEXT("李四"));
	TSharedPtr<UTreeItemData> LiSi1 = MakeShareable(new UTreeItemData());
	LiSi1->MyName = FString(TEXT("李四1"));
	TSharedPtr<UTreeItemData> LiSi2 = MakeShareable(new UTreeItemData());
	LiSi2->MyName = FString(TEXT("李四2"));
	TSharedPtr<UTreeItemData> LiSi3 = MakeShareable(new UTreeItemData());
	LiSi3->MyName = FString(TEXT("李四3"));
	
	LiSi->Children.Add(LiSi1);
	LiSi->Children.Add(LiSi2);
	LiSi->Children.Add(LiSi3);
	
	TreeItemDatas.Add(ZhangSan);
	TreeItemDatas.Add(LiSi);

	Arguments.TreeItemsSource(&TreeItemDatas);

	Arguments.OnGenerateRow_Raw(this, &SMyTreeView::GenerateRowItem);

	Arguments.OnGetChildren_Raw(this, &SMyTreeView::GetChildrenFromItem);

	//
	STreeView::Construct(Arguments);
}

TSharedRef<ITableRow> SMyTreeView::GenerateRowItem(TSharedPtr<UTreeItemData> InTreeItemData, const TSharedRef<STableViewBase>& OwnerTable) {
	return SNew(STableRow<TSharedPtr<UTreeItemData>>, OwnerTable)[
		SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()[
				SNew(STextBlock).Text(FText::FromString(InTreeItemData->MyName))
			]
			+ SHorizontalBox::Slot()[
				SNew(STextBlock).Text(FText::FromString(FString::SanitizeFloat(InTreeItemData->MyHeight)))
			]
		
	];
}

void SMyTreeView::GetChildrenFromItem(TSharedPtr<UTreeItemData> InTreeItem, TArray<TSharedPtr<UTreeItemData>>& OutChildren) {
	OutChildren = InTreeItem->Children;
}
